extends Control

@export var naviBtnDatas:Array[NaviData]
@onready var navi_container = %navi_container
@onready var naviBtnScene = preload("res://components/navigation.tscn")
@onready var naviBtnGroup = preload("res://assets/group/navi_btn_group.tres")
@onready var main_container = %main_container
@onready var navi_name_label = %navi_name_label
@onready var back_btn = %back_btn
@onready var settings_btn = %setting_btn
@onready var user_btn = %user_btn
@onready var logo_btn = %logoBtn

@export var naviShrinked = GlobalVariables.settingData.sidebarShrinked
var current_action:ActionData = null

## notification的panel
@onready var notification_panel_scene = preload("res://components/notification/notification_panel.tscn")
@onready var notification_vbcontainer = %notification_vbcontainer

var naviBtns = []

func _ready():
	bind_signals()
	init_navi_btns()

func bind_signals():
	Signalbus.naviBtnCilcked.connect(_on_navi_btn_clicked)
	Signalbus.new_action_created.connect(_on_new_action_created)
	Signalbus.action_back_requested.connect(_on_action_back_requested)
	Signalbus.notification_send.connect(_on_notification)
	
func init_navi_btns():
	var navi_index = 0
	for naviBtnData in naviBtnDatas:
		var naviBtn = naviBtnScene.instantiate()
		naviBtn.navi_btn_data = naviBtnData
		naviBtn.navi_index = navi_index
		naviBtn.button_group = naviBtnGroup
		if navi_index == 0:
			naviBtn.button_pressed = true
			Signalbus.naviBtnCilcked.emit(naviBtnData,navi_index)
		
		naviBtns.append(naviBtn)
		navi_container.add_child(naviBtn)
		navi_index +=1
	
	_add_serperate_navi_btn(settings_btn)
	_add_serperate_navi_btn(user_btn)
	# 如果naviShrinked是true
	if naviShrinked:
		_on_shrink_btn_pressed()

func _add_serperate_navi_btn(new_btn):
	new_btn.button_group = naviBtnGroup
	new_btn.navi_index = naviBtns.size()
	naviBtns.append(new_btn)

func select_navi_btn(navi_index:int):
	naviBtns[navi_index].button_pressed = true

func _on_shrink_btn_pressed():
	%AnimationPlayer.play("shrink")
	naviShrinked = true

func _on_logo_btn_pressed():
	if naviShrinked:
		%AnimationPlayer.play("open")
		naviShrinked = false

func _on_navi_btn_clicked(naviData:NaviData,navi_index:int):
	#load_new_page(naviData.navi_scene)
	Signalbus.new_action_created.emit(ActionData.new(naviData.navi_name,naviData.navi_scene,navi_index))
	
func load_new_page(newScene,param:Dictionary):
	clear_main_container()
	var naviPage = newScene.instantiate()
	if param.size()>0:
		for k in param.keys():
			naviPage.set(k,param.get(k))
	main_container.add_child(naviPage)

func clear_main_container():
	for child in main_container.get_children():
		main_container.remove_child(child)
		child.queue_free()

func _on_new_action_created(actionData:ActionData):
	#做一个检查，如果前面的action不为空 输入history
	if current_action!=null:
		GlobalVariables.action_history.append(current_action)
	#检查历史大小
	check_back_btn_state()
	# 更新当前action的大小
	current_action = actionData
	update_navi(actionData)

func _on_action_back_requested(actionData:ActionData):
	update_navi(actionData)
	# 更新当前的current_action
	current_action = actionData
	# 更新当前选中的导航按钮
	select_navi_btn(actionData.navi_index)

func check_back_btn_state():
	back_btn.disabled = GlobalVariables.action_history.size() == 0

func update_navi(actionData:ActionData):
	navi_name_label.text = actionData.action_name
	load_new_page(actionData.action_scene,actionData.action_param)

func _on_back_pressed():
	if GlobalVariables.action_history.size()==0:
		return
	if GlobalVariables.action_history.size()>0:
		var preActionData = GlobalVariables.action_history.pop_back()
		Signalbus.action_back_requested.emit(preActionData)
		check_back_btn_state()

func _notification(what):
	if what == NOTIFICATION_WM_WINDOW_FOCUS_IN:
		if OS.get_name() == 'IOS' or OS.get_name() == 'Android':
			print("Window maximized or restored")
			GlobalVariables.async_data()

func _on_notification(msg:String):
	show_notification(msg)

func show_notification(msg:String,msg_type:GlobalVariables.NOTIFICATION_TYPE = GlobalVariables.NOTIFICATION_TYPE.INFO):
	var notification_panel = notification_panel_scene.instantiate()
	notification_panel.msg = msg
	notification_panel.notification_type = msg_type
	notification_vbcontainer.add_child(notification_panel)
